capture prog drop WADdraw
program WADdraw, rclass

syntax, [iter(real 1) reps(real 1000) bwy(real 5) year(real 57) pctu(real 99) pctl(real 1) ql(string) outcome(string) root(string)]

local bw=365*`bwy'
local t=(`year'-57)*365

set seed `iter'

cap log close
log using "`root'/Stats_`bwy'yr_`ql'_t`year'/iter`iter'_`outcome'_pctu`pctu'.log"

cd "`root'"
use "Data_for_Hypothesis_Test.dta", clear

replace DoB=DoB-`t'
keep if abs(DoB)<`bw'
keep if `outcome'==1
keep DoB

gen thestats=.
forvalues i=1/`reps' {
	gen testtrait=rnormal()
	
	WAD_ROSLA_RF_gen_`ql' testtrait DoB, g(test) bw(`bw') pctl(`pctl') pctu(`pctu')
	sum test
	replace thestats=r(mean) in `i'
	drop test testQ testtrait
	disp `i'
}
drop if thestats==.
keep thestats*

save "`root'/Stats_`bwy'yr_`ql'_t`year'/Wad_stats_`bwy'yr_`ql'_t`year'_`outcome'_pctl`pctl'_pctu`pctu'_`iter'.dta", replace
log close

return scalar done=1

end
